{% macro property_list(name, data) %}
<dl>
{% for k, v in data %}
    <dt>{{ k }}</dt>
    <dd class="text-wrap">
        {% if not v is iterable %}
          {{ v|join(' ') }}
        {% else %}
          {{ v|json_encode() }}
        {% endif %}
    </dd>
{% else %}
    <dt>No {{ name }} data</dt>
{% endfor %}
</dl>
{% endmacro %}

{% macro sort_link(text, url, sort, paging, search) %}
{% set search = search|default({}) %}
{% set classname = '' %}
{% set direction = 'asc' %}
{%- if paging.direction == 'asc' -%}
    {% set direction = 'desc' %}
{%- endif -%}
{% if paging.sort == sort %}
    {% set classname = "sort-#{direction}" %}
{% endif %}
{% set params = {'direction': direction, 'sort': sort}|merge(search) %}

<a class="sort-link {{ classname }}" href="{{ url(url, params) }}">{{ text }}</a>
{% endmacro %}

{% macro tip_link(text, len, url, params) %}
<a {% if text|length > len %}class="tip"{% endif %}
    href="{{ url(url, params) }}"
    title="{{ text }}">
    {{ text|truncate(len) }}
</a>
{% endmacro %}

{% macro pagination(url, paging, search) %}

{# Hard code because I'm lazy #}
{% set num_pages = 10 %}
{% set midpoint = none %}
{% set start = 1 %}
{% set end = paging.total_pages|default(start) %}
{% set total_pages = paging.total_pages|default(start) %}
{% set search = search|default({}) %}
{% set search = search|merge({direction: paging.direction, sort: paging.sort}) %}

{# show a slice of pages in the middle #}
{% if total_pages > num_pages %}
    {% set midpoint = (num_pages // 2) %}
    {% set start = paging.page - midpoint %}
    {% if start < 1 %}
        {% set start = 1 %}
    {% endif %}
    {% set end = paging.page + midpoint %}
    {% if end < num_pages %}
        {% set end = num_pages %}
    {% endif %}
    {% if end > total_pages %}
        {% set end = total_pages %}
    {% endif %}
{% endif %}


<div class="pagination">
<ul>
{% if paging.page == 1 %}
    <li class="disabled"><span>&laquo;</span><li>
{% else %}
    <li><a href="{{ url(url, search|merge({'page': paging.page - 1})) }}">&laquo;</a><li>
{% endif %}

{% for i in start..end %}
    {% if i == paging.page %}
    <li class="active"><span>{{ i }}</span></li>
    {% else %}
    <li><a href="{{ url(url, search|merge({'page': i})) }}">{{ i }}</a></li>
    {% endif %}
{% endfor %}

{% if paging.page == total_pages %}
    <li class="disabled"><span>&raquo;</span></li>
{% else %}
    <li><a href="{{ url(url, search|merge({'page': paging.page + 1})) }}">&raquo;</a></li>
{% endif %}
</ul>
</div>
{% endmacro %}

{% macro time_and_percent(prop, value, total) %}
{{ value[prop]|as_time }}<br />
({{ percent(value[prop], total[prop]) }})
{% endmacro %}

{% macro bytes_and_percent(prop, value, total) %}
{{ value[prop]|as_bytes }}<br />
({{ percent(value[prop], total[prop]) }})
{% endmacro %}
